import random
import numpy as np
import pandas as pd

import sys
import pyhdfe
import pyblp
import statsmodels.api as sm


np.__version__
pd.__version__
pyblp.__version__
sm.__version__

 
product_module_code = str(sys.argv[1])
year = str(sys.argv[2])
path_head = str(sys.argv[3])
path = path_head + product_module_code + '_' + year + '.csv'
product_data1 = pd.read_csv(path)


product_data1['xi'] = product_data1.delta - product_data1.alpha[0]*product_data1.prices
beta_0 = np.mean(product_data1.xi)
product_data1['xi'] = product_data1['xi']-beta_0
product_data1["1"] = 1
product_data1["x1"] = 0

sim = pyblp.Simulation(
	product_formulations = (
		pyblp.Formulation('1 + x1 + prices')
	),
    product_data = product_data1,
    rho = product_data1.sigma[0],
    beta = np.array([[beta_0,0.00001,product_data1.alpha[0]]]).T,
    xi = product_data1.xi
)

sim_results = sim.replace_exogenous(X1_name = 'x1', delta = product_data1.delta, iteration=pyblp.Iteration('return'))
sim_results.product_data['x1'] = 0
costs = sim_results.compute_costs()
product_data1["costs"] = costs
product_data1.to_csv(path)
